<?php

namespace App\Api\Controllers;

use App\Api\Dao\UserDao;
use App\Api\Logic\TestLogic;
use App\Helpers\Lib\AliSmsServices;
use App\Helpers\Lib\FuyouPay;
use App\Helpers\Lib\RedisService;
use App\Helpers\Lib\Shiyi;
use App\Helpers\Lib\WechatMini;
use App\Helpers\Lib\WechatPay;
use App\Helpers\ResponseEnum;
use App\Models\TongOrder;
use App\Models\TongUser;
use App\Models\TongUserMonthMoney;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class IndexController extends BaseController
{
    /**
     * 测试123456
     * @return \Illuminate\Http\JsonResponse
     */
    public function test(Request $request,TestLogic $testLogic,UserDao $userDao){
        //echo 123456;exit();
//        resetRedisData();
//        echo '重置用户信息成功';
//        exit();
        statistics_month_money();
        echo '管理发放完毕';
        exit;

        $month_start = date('Y-m-01', strtotime(date("Y-m-d"))); //本月开始时间
        $month_end   = date('Y-m-d', strtotime("$month_start +1 month -1 day")).' 23:59:59'; //本月结束时间
        var_dump($month_start);
        var_dump($month_end);
        echo '执行完毕';
        exit();
//        new_area_money(5541,709);
//        exit();
        //echo config("reward.leadership_rewards");
//        FuyouPay::serviceOPenId();
//        echo '获取openid';
//        exit();
        $res = FuyouPay::createPay(1,'1066'.makeOrderNo());
        return $this->success($res);
        exit();
        $res = FuyouPay::miniPay('ooIeqs-Pk6r8jU4XVwkzDOBGbdlk',1,'1066'.makeOrderNo());
        return $this->success($res);
        exit();
        $leader_id = find_leader_user(4494);
        leaderReward(4494,599);
        echo $leader_id.'领导人id'.$leader_id;
        exit();
        //leaderReward(5067,1);
        //echo '执行完毕';
        exit($leader_id);exit();
        //exit('找到领导人'.$leader_id);
        WechatMini::getAccessToken();
        exit('重置accesstoken');
        dd();
        resetRedisData();
        echo '重置用户信息完毕';
        exit();
//        AliSmsServices::sendSms(19933109733,6666);
//        exit();
        $user_query = TongUser::query()->whereIn('identity_id',[4,5]);
        if(!$user_query->exists()){
            echo '没有找到此类用户';
        }
        $user_list = $user_query->get()->toArray();
        DB::beginTransaction();
        try {
            foreach ($user_list as $k=>$v){
                TongUser::query()->where('id',$v['id'])->increment('identity_id');
            }
            DB::commit();
        }catch (\Exception $exception){
            DB::rollBack();
        }


        echo '共'.count($user_list).'打印完毕';
        exit();
        //statistics_month_money();
       // restUser();
//        $all_user_data = [];
//        TongUser::query()->each(function ($item) use(&$all_user_data){
//            $all_user_data[] = [
//                'u_id'=>$item['id'],
//                'child_month_order'=>$item['child_month_order'],
//                'p_id'=>$item['p_id'],
//                'status'=>1,
//                'identity_id'=>$item['identity_id'],
//                'created_at'=>date('Y-m-d H:i:s',time()),
//                'updated_at'=>date('Y-m-d H:i:s',time()),
//            ];
//        });
//        //将用户业绩复制出来
//        TongUserMonthMoney::query()->insert($all_user_data);
        //将用户的业绩归0
        //TongUser::query()->update(['child_month_order'=>0]);
        echo '重置完毕11';
        exit();
//        $res = getUserZAndJ(6);
//        dd($res);
        resetRedisData();
        exit();
//        echo 'redis重置完毕';exit();
//        restUser();
//        echo '关系重置完毕';
//        exit();
        //dd('123');
       // echo '开始导入用户数据';exit();
        $new_user_data = [];
        //用来存储新的用户信息
        DB::table('fa_user')->orderBy('id','asc')->chunk(50,function ($old_user) use(&$new_user_data) {
            foreach ($old_user as $key => $val) {

                if (is_null($val->blind_user)) {
                    //查询用户的邀请人是否存在

                    //$new_user_data
                    $new_user_data[] = [
                        'id' => $val->id,
                        'nickname' => is_null($val->nickname) ? '' : $val->nickname,//用户昵称
                        'identity_id' => $val->group_id - 1,//用户身份标识
                        'mobile' => empty($val->mobile) ? generateRandomPhoneNumber() : $val->mobile,//用户手机号
                        'money' => $val->money, //钱包余额
                        'repeat_money' => $val->score,//复购券
                        'share_code' => createShareCode(),//分享吗
                        'password'=>'123456',//默认登录密码
                        'account' => $val->username,
                        'p_id' => 0,//上级id
                        'created_at' => date('Y-m-d H:i:s', $val->createtime),
                        'updated_at' => date('Y-m-d H:i:s', $val->updatetime),
                    ];

                } else {
                    $p_query = Db::table('fa_user')->where('username', $val->blind_user);
                    if ($p_query->exists()) {
                        $p_info = $p_query->first();
                        $p_id = $p_info->id;
                    } else {
                        $p_id = 0;
                    }
                    $new_user_data[] = [
                        'id' => $val->id,
                        'nickname' => is_null($val->nickname) ? '' : $val->nickname,//用户昵称
                        'identity_id' => $val->group_id - 1,//用户身份标识
                        'mobile' => empty($val->mobile) ? generateRandomPhoneNumber() : $val->mobile,//用户手机号
                        'money' => $val->money, //钱包余额
                        'repeat_money' => $val->score,//复购券
                        'share_code' => createShareCode(),//分享吗
                        'password'=>'123456',//默认登录密码
                        'account' => $val->username,
                        'p_id' => $p_id,//上级id
                        'created_at' => date('Y-m-d H:i:s', $val->createtime),
                        'updated_at' => date('Y-m-d H:i:s', $val->updatetime),
                    ];

                }
            }


        });
        //dd($new_user_data);
        $res = DB::table('tong_users')->insert($new_user_data);
        if($res){
            echo '导入成功';
        }else{
            echo '导入失败';
        }

        //(new WechatPay())->to_pay(['order_no'=>time(),'description'=>'购买商品','openid'=>'oHjii69Rtrq6CFp2f7Mml7UHWXl4']);
        //WechatMini::createExpressOrder([]);
        //$res = TongUser::query()->where('id',1330)->pluck('id')->toArray();
        //var_dump($res);
//        $user_list = getRedisUserData();
//        dd($user_list);
       // RedisService::delAll();
        //restUser();
        //Redis::connection()->client()->hSet('test','field1',123456);
        //RedisService::(68,['id'=>68,'p_id'=>67,'identity_id'=>3,'is_real_store'=>-1]);
        //Redis::connection()->client()->h
        //restUser();exit();
        //$res = RedisService::hGet(77);
        //dd($res);
        //updateUserRealStore(146,-1);
        //$res = getRedisUserData();
        //var_dump($res);
        //dd($res);
       // RedisService::hUpdate(68,['id'=>68,'p_id'=>67,'identity_id'=>3,'is_real_store'=>-1]);
       // $res = RedisService::getAlls();
        //dd($res);
        //var_dump(count($res));
        //var_dump($res);
       // restUser();exit();
//        $arr = ['id'=>123,'name'=>'张三'];
//        Redis::connection()->client()->hSet('test','filed1',json_encode($arr));
//        //删除掉字段
//        Redis::connection()->client()->hDel('test','filed1');
//        $arr = ['id'=>12,'name'=>'李四'];
//        Redis::connection()->client()->hSet('test','filed1',json_encode($arr));
//        $res = Redis::connection()->client()->hGetAll('test');
//        $resData = [];
//        foreach ($res as $k=>$v){
//            //var_dump($k);
//            $resData[] = json_decode($v,true);
//        }
//        dd($resData);
        //var_dump($res);
        //getCompanyTeam();
        //dd($str_arr);
       // restUser();exit();
        //$res = isSmallToBig(578);
        //dd($res);
        //price_difference_upgrade_tow(162,578);
       // $old = ['id'=>136,'p_id'=>135,'identity_id'=>3,'is_real_store'=>-1];
       // ksort($old);
//        var_dump($old);
       // RedisService::dleItem($old);
        //isSmallToBig(578);
        //dd($res);
        //$test=  [78,77,63,57,51,36,1];
//        RedisService:: sAdd(['id'=>3,'name'=>'王五','p_id'=>0]);
//        RedisService::delAll();
//        $res = RedisService::getAlls();
//        dd($res);
        //restUser();exit();
//        $data = $request->all();
//        $res = countUserUpgrade($data['id']);
//        dd($res);
        //cultivation_money(32,6);
        //if($request->all())
//        $data = $request->all();
//        if(checkIsHasField($data,'type')){
//            restUser();exit();
//        }else{
//            recommend_money_child_cha(38,'2-1');
//        }

        //$user_list = getRedisUserData();
        //$all_parents = getAllParents($user_list, 32);
        //dd($user_list,$all_parents);
        //recommend_money_child_cha(38,'2-1');
        //restUser();exit();
       // recommend_money_child_cha(38,'2-1');
//        $user_list = getRedisUserData();
//
//        $all_parents = getAllParents($user_list, 38);
//        dd($user_list,$all_parents);
        //restUser();exit();
        //recommend_money_child_cha(42,'2-2');
        //isFirstOrder(4);
        getUserShareCount(2);
        //restUser();exit();
//        $today_start = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
//        $today_end   = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
//        echo $today_start;
//        echo $today_end;
        //WechatMini::createExpressOrder();
//        $res = WechatMini::getDelivery();
//        $new_res = [];
//        foreach ($res as $k=>$v){
//            $new_res[] = [
//                'id'=>$v['delivery_id'],
//                'text'=>$v['delivery_name']
//            ];
//        }
//        dd($new_res);
        //echo route('wechatPayNotice');
        //grade_difference(25);
        //restUser();exit();
        //grade_difference();
       // $data = $request->all();
//        restUser();exit();
//        $data = $request->all();
        //(new WechatPay())->to_pay($data);
       // restUser();exit();
//        $data = $request->all();
//        $res = WechatMini::getOpenId($data['code']);
//        var_dump($res);
        //echo Cache::store('redis')->get('tongxianshengAccess');
        //WechatMini::getAccessToken();
        //$params= $request->all();
        //echo WechatMini::getAccessToken();
        //getCompanyTeam();
        //restUser();exit();
        //$user_list = getRedisUserData();
       // var_dump($user_list);
        //$user_list = getRedisUserData();
        //var_dump($user_list);
        //updateUserIdentity(2,1);
        //restUser();exit();
        //grade_difference(85);
        //平级奖励测试
        //cultivation_money(110,100);
        //测试差价升级
        //price_difference_upgrade(110);
        //area_money(74);
        //$currentDateTime = Carbon::now();
        //echo $currentDateTime;
        //echo date('Y-m-d H:i:s',time());
        //restUser();exit();
        //createMoreUser(5,105,1);
        //store_money(110);
        //recommend_money(23);
        // repurchase_money(23);
        //repurchase_money(25);
        //getPartnerTeam();
        //userUpgrade(74);exit();
       // restUser();exit();
        //getCompanyTeam();
        //var_dump($result);exit();
        //$res = getTodayOrderMoney();
        //var_dump($res);
        //exit();
       //restUser();exit();
        //
        //createMoreUser(1,2,2);
       //  userUpgrade(74);
      //  echo '123';
       // createMoreUser(3,2);
       // createMoreOrder(71,14);
       // echo '成功';


//        $res = getRedisUserData();
//        //$res = getChilds($res,1);
//        $res = getParentLevel($res,42,3);
//        var_dump($res);exit();
//        //resetRedisData();
//        //Cache::store('redis')->put('55',['123'=>'456']);
//        $res = Cache::store('redis')->get('55');
//        var_dump($res);
//        echo '设置成功';
//        exit();
//        $query = TongUser::query();
//        $pInfo = $query->where('share_code','35C198')->first()->toArray();
//        //var_dump($pInfo);exit();
//        $userDao->updateThreeChildInfo(33,$pInfo);
//        exit();
//        reset_child_user();
//        exit();
//        $user_list = TongUser::query()->get(['id','p_id','child_id'])->toArray();
//        //var_dump($user_list);
//        $parents   = getLevelTopPid($user_list, 23,3);
//        var_dump($parents);exit();
//        $res = get_child_order_count(1);
//        var_dump($res);exit();
//        return $this->success($res);
//        //var_dump($res);
//        exit();
//        echo 123;
//        var_dump($request->u_id);
//        //接口成功返回
//        return $this->success(['123']);
        //接口错误返回
        //return $this->fail(ResponseEnum::USER_SERVICE_LOGIN_ERROR);
        //异常抛出
        //$this->throwBusinessException(ResponseEnum::USER_ACCOUNT_REGISTERED,114651616);
        //$testLogic->login([]);
    }
}
